package com.taotao.manage.controller.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;
import com.taotao.common.bean.EasyUIResult;
import com.taotao.manage.pojo.Content;
import com.taotao.manage.service.ContentService;

@Controller
@RequestMapping("api/content")
public class ApiContentController {

	@Autowired
	private ContentService contentService;

	/**
	 *	根据分类ID查询所有内容
	 */
	@RequestMapping(method = RequestMethod.GET)
	public ResponseEntity<EasyUIResult> queryContentByPage(@RequestParam("categoryId") Long categoryId,
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "30") Integer rows) {
		try {

			EasyUIResult easyUIResult = new EasyUIResult();

			Content record = new Content();
			record.setCategoryId(categoryId);
			//分页查询，并根据更新时间排序
			PageInfo<Content> pageInfo = contentService.queryPageListByWhere(record, page, rows);
			if (pageInfo == null || pageInfo.getSize() == 0) {
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}
			easyUIResult.setTotal(pageInfo.getTotal());
			easyUIResult.setRows(pageInfo.getList());
			return ResponseEntity.status(HttpStatus.OK).body(easyUIResult);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);

	}

}
